package com.bosssoft.springcloud.controller;

import com.bosssoft.springcloud.entity.dto.MenuDTO;
import com.bosssoft.springcloud.entity.dto.RoleDTO;
import com.bosssoft.springcloud.entity.vo.CommenResult;
import com.bosssoft.springcloud.entity.vo.MenuVO;
import com.bosssoft.springcloud.entity.vo.RoleVO;
import com.bosssoft.springcloud.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class MenuController {
    @Resource
    private MenuService menuService;

    /***
     * 获取角色菜单
     * @param roleVO 角色
     * @return
     */
    @GetMapping("/rbac/menu/getAll")
    public CommenResult getMenuByRoleId(RoleVO roleVO){
        RoleDTO roleDTO = new RoleDTO();
        BeanUtils.copyProperties(roleDTO,roleVO);
        List<MenuDTO> res = menuService.getMenuByRoleId(roleDTO.getRid());
        if (res!=null&&res.size()>0){
            return new CommenResult(200,"查询成功",res);
        }
        return new CommenResult(404,"查询失败",null);
    }

    /***
     * 添加菜单
     * @param menuVO 菜单
     * @return
     */
    @GetMapping("/rbac/menu/addMenu")
    public CommenResult addMenu(MenuVO menuVO){
        MenuDTO menuDTO = new MenuDTO();
        BeanUtils.copyProperties(menuDTO,menuVO);
        int res = menuService.addMenu(menuDTO);
        if (res > 0){
            return new CommenResult(200,"插入成功",res);
        }
        return new CommenResult(404,"插入失败",null);
    }

    /***
     * 移除菜单
     * @param menuVO 菜单
     * @return
     */
    @GetMapping("/rbac/menu/removeMenu")
    public CommenResult removeMenu(MenuVO menuVO){
        MenuDTO menuDTO = new MenuDTO();
        BeanUtils.copyProperties(menuDTO,menuVO);
        int res = menuService.removeMenu(menuDTO.getId());
        if (res > 0){
            return new CommenResult(200,"移除成功",res);
        }
        return new CommenResult(404,"移除失败",null);
    }
}
